var http = require("http.js");

/**处理浮点数，保留两位小数 */
function keepTwoDecimal(num) {
  var result = parseFloat(num);
  if (isNaN(result)) {
    wx.showToast({
      title: '传递参数错误，请检查！',
    });
    return false;
  }
  result = Math.round(num * 100) / 100;
  return result;
}

function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}

function dec2hex(dec,length)
{
  let hex = parseInt(dec).toString(16)
  while(hex.length<length)
  {
    hex='0'+hex
  }
  return hex
}

function hex2bin(hex, length) {
  let bin = parseInt(hex, 16).toString(2)
  while (bin.length < length) {
    bin = '0' + bin
  }
  return bin
}


/**处理实时数据，第一通道腰部，第二通道背部，低12位才是数据 */
function dealRealTimeCommand(cmdContent) {
  var that = this;
  var list = {};
  var temp = hex2bin(cmdContent.substr(16, 4),16)
  var back = parseInt(temp.substr(4, 12), 2).toString(10);
  back = keepTwoDecimal(27887 * Math.pow(back, -1.581));
  temp = hex2bin(cmdContent.substr(12, 4),16)
  var waist = parseInt(temp.substr(4, 12), 2).toString(10);
  waist = keepTwoDecimal(19417 * Math.pow(waist, -1.541));
  list.lumbarPressure = waist;
  list.backPressure = back;
  list.measureTime = new Date().toLocaleString();
  return list;
}

/**处理上传的历史数据 */
function dealHistoryData(cmdContent)
{
  console.log(content)
}

/**获取对时指令 */
function getSynchronizeDateCmd()
{
  let buffer = new ArrayBuffer(16)
  let dataView = new DataView(buffer)
  dataView.setUint8(0, 0xff)
  dataView.setUint8(1, 0x05)
  var oDate = new Date();
  var temp=dec2hex(oDate.getFullYear(),4)
  dataView.setUint8(2, parseInt(temp.substr(0,2),16))
  dataView.setUint8(3, parseInt(temp.substr(2, 2),16))
  dataView.setUint8(4, (oDate.getMonth()+1))
  dataView.setUint8(5, oDate.getDate())
  dataView.setUint8(6, (oDate.getHours()))
  dataView.setUint8(7, oDate.getMinutes())
  dataView.setUint8(8, oDate.getSeconds())
  dataView.setUint8(9, (oDate.getDay()))
  dataView.setUint8(14, 0x0d)
  dataView.setUint8(15, 0x0a)
  return buffer
}

/**获取标定数据指令 1：未穿戴，2：松，3：合适，4:紧*/
function getCalibrationDataCmd(calibrationType, waistData,backData)
{
  let buffer = new ArrayBuffer(16)
  let dataView = new DataView(buffer)
  if (calibrationType=='1')
  {
    dataView.setUint8(0, 0xff)
    dataView.setUint8(1, 0x07)
    dataView.setUint8(2, 0x04)
    let temp = dec2hex(waistData,4)
    dataView.setUint8(3, parseInt(temp.substr(0, 2)))
    dataView.setUint8(4, parseInt(temp.substr(2, 2)))
    temp = dec2hex(backData, 4)
    dataView.setUint8(5, parseInt(temp.substr(0, 2)))
    dataView.setUint8(6, parseInt(temp.substr(2, 2)))
    dataView.setUint8(14, 0x0d)
    dataView.setUint8(15, 0x0a)
  }
  return buffer
}

/**获取上传历史数据指令 */
function getHistoryDataCmd(lastMeasureTime)
{
  let buffer = new ArrayBuffer(16)
  if(lastMeasureTime!=null && lastMeasureTime!='')
  {
    let dataView = new DataView(buffer)
    dataView.setUint8(0, 0xff)
    dataView.setUint8(1, 0x03)
    var temp = dec2hex(parseInt(lastMeasureTime.substr(0, 4)), 4)
    dataView.setUint8(2, parseInt(temp.substr(0, 2), 16))
    dataView.setUint8(3, parseInt(temp.substr(2, 2), 16))
    dataView.setUint8(4, parseInt(lastMeasureTime.substr(5, 2)))
    dataView.setUint8(5, parseInt(lastMeasureTime.substr(8, 2)))
    dataView.setUint8(14, 0x0d)
    dataView.setUint8(15, 0x0a)
  }
  return buffer
}

module.exports = {
  dealRealTimeCommand: dealRealTimeCommand,
  dealHistoryData: dealHistoryData,
  getCalibrationDataCmd: getCalibrationDataCmd,
  getHistoryDataCmd: getHistoryDataCmd,
  getSynchronizeDateCmd: getSynchronizeDateCmd
}